package com.timothy.fund.ui;

import java.util.ArrayList;
import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.timothy.fund.adapter.FundCatagoryAdaptor;
import com.timothy.fund.bean.FundCatagory;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

public class MainActivity extends SherlockActivity {
	public final static String LOG_TAG = "MainActivity";
	public static int THEME = R.style.Theme_Sherlock;
	
	List<FundCatagory> fcList = new ArrayList<FundCatagory>();
	GridView gridList;
    Context mContext;
    GridViewAdapter adapter;
//    FundCatagoryAdaptor adapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	setTheme(Simple.THEME); //Used for theme switching in samples
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        
        mContext = getApplicationContext();
        
        //initial fund catagory
        fcList.add(new FundCatagory("Stock A","Stock A"));
        fcList.add(new FundCatagory("Stock B","Stock B"));
        fcList.add(new FundCatagory("Repo","Repo"));

        
        //set List
        gridList = (GridView)findViewById(R.id.gridview);
        adapter = new GridViewAdapter(fcList);
//        adapter = new FundCatagoryAdaptor(fcList,mContext);
        gridList.setAdapter(adapter);
        gridList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
                Intent intent = new Intent();
//    				intent.setClass( mContext, StockAListActivity.class);
                intent.setClass( mContext, RepoListActivity.class);
                startActivity(intent);
				
			}      	
        });
        
    }

    
    class GridViewAdapter extends BaseAdapter{
    	List<FundCatagory> fcList = new ArrayList<FundCatagory>();
    	private LayoutInflater mInflater;
    	
    	public GridViewAdapter(List<FundCatagory> fcList){
    		this.fcList = fcList;
    		mInflater = LayoutInflater.from(mContext);  
    	}

		@Override
		public int getCount() {
			return fcList.size();
		}

		@Override
		public Object getItem(int position) {
			return fcList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(LOG_TAG,"getView()..."+" position="+position);
        	
			ViewHolder holder;  
            if (convertView == null){  
                holder = new ViewHolder();  
                convertView = mInflater.inflate(R.layout.main_item, null);  
                holder.fCatagory = (Button) convertView.findViewById(R.id.fundCategory);  
                convertView.setTag(holder);  
            }else {  
                holder = (ViewHolder) convertView.getTag();  
            }  
            
            FundCatagory fc = fcList.get(position);
            
            holder.fCatagory.setText(fc.getName());  
            holder.fCatagory.setOnClickListener(new Button.OnClickListener() {
    			public void onClick(View v) {
    				Log.i(LOG_TAG,"onClick()...");
//    				Button sButton = (Button)v;
//    	            String text = sButton.getText().toString();
//    	            Intent intent = new Intent();
//    				if(Catagory.STOCK_A.equals(text)){
//    					intent.setClass( mContext, Simple.class);
//    				}else if(Catagory.STOCK_B.equals(text)){
//    					intent.setClass( mContext, Simple.class);
//    				}else{
//    					//ShowDialog(mContext.getResources().getString(R.string.dialog_note),"Invalid qeustion type!");
//    				}
//    				finish();
//    				startActivity(intent);
    				
    				Intent intent = new Intent();
//    				intent.setClass( mContext, StockAListActivity.class);
    				intent.setClass( mContext, RepoListActivity.class);
    				startActivity(intent);
    			}
    		});
            return convertView; 
		}
    	
    }
    
    static class ViewHolder{
    	Button fCatagory;
    }
}
